﻿namespace log4net.Util
{
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Globalization;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Security;
    using System.Threading;

    public sealed class SystemInfo
    {
        private static readonly Type declaringType = typeof(SystemInfo);
        private const string DEFAULT_NOT_AVAILABLE_TEXT = "NOT AVAILABLE";
        private const string DEFAULT_NULL_TEXT = "(null)";
        public static readonly Type[] EmptyTypes = new Type[0];
        private static string s_appFriendlyName;
        private static string s_hostName;
        private static string s_notAvailableText;
        private static string s_nullText;
        private static DateTime s_processStartTime = DateTime.Now;

        static SystemInfo()
        {
            string str = "(null)";
            string str2 = "NOT AVAILABLE";
            string appSetting = GetAppSetting("log4net.NullText");
            if ((appSetting != null) && (appSetting.Length > 0))
            {
                LogLog.Debug(declaringType, "Initializing NullText value to [" + appSetting + "].");
                str = appSetting;
            }
            string str4 = GetAppSetting("log4net.NotAvailableText");
            if ((str4 != null) && (str4.Length > 0))
            {
                LogLog.Debug(declaringType, "Initializing NotAvailableText value to [" + str4 + "].");
                str2 = str4;
            }
            s_notAvailableText = str2;
            s_nullText = str;
        }

        private SystemInfo()
        {
        }

        public static string AssemblyFileName(Assembly myAssembly)
        {
            return Path.GetFileName(myAssembly.Location);
        }

        public static string AssemblyLocationInfo(Assembly myAssembly)
        {
            if (myAssembly.GlobalAssemblyCache)
            {
                return "Global Assembly Cache";
            }
            try
            {
                return myAssembly.Location;
            }
            catch (SecurityException)
            {
                return "Location Permission Denied";
            }
        }

        public static string AssemblyQualifiedName(Type type)
        {
            return (type.FullName + ", " + type.Assembly.FullName);
        }

        public static string AssemblyShortName(Assembly myAssembly)
        {
            string fullName = myAssembly.FullName;
            int index = fullName.IndexOf(',');
            if (index > 0)
            {
                fullName = fullName.Substring(0, index);
            }
            return fullName.Trim();
        }

        public static string ConvertToFullPath(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            string localPath = "";
            try
            {
                string applicationBaseDirectory = ApplicationBaseDirectory;
                if (applicationBaseDirectory != null)
                {
                    Uri uri = new Uri(applicationBaseDirectory);
                    if (uri.IsFile)
                    {
                        localPath = uri.LocalPath;
                    }
                }
            }
            catch
            {
            }
            if ((localPath != null) && (localPath.Length > 0))
            {
                return Path.GetFullPath(Path.Combine(localPath, path));
            }
            return Path.GetFullPath(path);
        }

        public static ArgumentOutOfRangeException CreateArgumentOutOfRangeException(string parameterName, object actualValue, string message)
        {
            return new ArgumentOutOfRangeException(parameterName, actualValue, message);
        }

        public static Hashtable CreateCaseInsensitiveHashtable()
        {
            return new Hashtable(StringComparer.OrdinalIgnoreCase);
        }

        public static string GetAppSetting(string key)
        {
            try
            {
                return ConfigurationManager.AppSettings[key];
            }
            catch (Exception exception)
            {
                LogLog.Error(declaringType, "Exception while reading ConfigurationSettings. Check your .config file is well formed XML.", exception);
            }
            return null;
        }

        public static Type GetTypeFromString(string typeName, bool throwOnError, bool ignoreCase)
        {
            return GetTypeFromString(Assembly.GetCallingAssembly(), typeName, throwOnError, ignoreCase);
        }

        public static Type GetTypeFromString(Assembly relativeAssembly, string typeName, bool throwOnError, bool ignoreCase)
        {
            if (typeName.IndexOf(',') == -1)
            {
                Type type = relativeAssembly.GetType(typeName, false, ignoreCase);
                if (type != null)
                {
                    return type;
                }
                Assembly[] assemblies = null;
                try
                {
                    assemblies = AppDomain.CurrentDomain.GetAssemblies();
                }
                catch (SecurityException)
                {
                }
                if (assemblies != null)
                {
                    foreach (Assembly assembly in assemblies)
                    {
                        type = assembly.GetType(typeName, false, ignoreCase);
                        if (type != null)
                        {
                            LogLog.Debug(declaringType, "Loaded type [" + typeName + "] from assembly [" + assembly.FullName + "] by searching loaded assemblies.");
                            return type;
                        }
                    }
                }
                if (throwOnError)
                {
                    throw new TypeLoadException("Could not load type [" + typeName + "]. Tried assembly [" + relativeAssembly.FullName + "] and all loaded assemblies");
                }
                return null;
            }
            return Type.GetType(typeName, throwOnError, ignoreCase);
        }

        public static Type GetTypeFromString(Type relativeType, string typeName, bool throwOnError, bool ignoreCase)
        {
            return GetTypeFromString(relativeType.Assembly, typeName, throwOnError, ignoreCase);
        }

        public static Guid NewGuid()
        {
            return Guid.NewGuid();
        }

        public static bool TryParse(string s, out short val)
        {
            val = 0;
            try
            {
                double num;
                if (double.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out num))
                {
                    val = Convert.ToInt16(num);
                    return true;
                }
            }
            catch
            {
            }
            return false;
        }

        public static bool TryParse(string s, out int val)
        {
            val = 0;
            try
            {
                double num;
                if (double.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out num))
                {
                    val = Convert.ToInt32(num);
                    return true;
                }
            }
            catch
            {
            }
            return false;
        }

        public static bool TryParse(string s, out long val)
        {
            val = 0;
            try
            {
                double num;
                if (double.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out num))
                {
                    val = Convert.ToInt64(num);
                    return true;
                }
            }
            catch
            {
            }
            return false;
        }

        public static string ApplicationBaseDirectory
        {
            get
            {
                return AppDomain.CurrentDomain.BaseDirectory;
            }
        }

        public static string ApplicationFriendlyName
        {
            get
            {
                if (s_appFriendlyName == null)
                {
                    try
                    {
                        s_appFriendlyName = AppDomain.CurrentDomain.FriendlyName;
                    }
                    catch (SecurityException)
                    {
                        LogLog.Debug(declaringType, "Security exception while trying to get current domain friendly name. Error Ignored.");
                    }
                    if ((s_appFriendlyName == null) || (s_appFriendlyName.Length == 0))
                    {
                        try
                        {
                            s_appFriendlyName = Path.GetFileName(EntryAssemblyLocation);
                        }
                        catch (SecurityException)
                        {
                        }
                    }
                    if ((s_appFriendlyName == null) || (s_appFriendlyName.Length == 0))
                    {
                        s_appFriendlyName = s_notAvailableText;
                    }
                }
                return s_appFriendlyName;
            }
        }

        public static string ConfigurationFileLocation
        {
            get
            {
                return AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            }
        }

        public static int CurrentThreadId
        {
            get
            {
                return Thread.CurrentThread.ManagedThreadId;
            }
        }

        public static string EntryAssemblyLocation
        {
            get
            {
                return Assembly.GetEntryAssembly().Location;
            }
        }

        public static string HostName
        {
            get
            {
                if (s_hostName == null)
                {
                    try
                    {
                        s_hostName = Dns.GetHostName();
                    }
                    catch (SocketException)
                    {
                    }
                    catch (SecurityException)
                    {
                    }
                    if ((s_hostName == null) || (s_hostName.Length == 0))
                    {
                        try
                        {
                            s_hostName = Environment.MachineName;
                        }
                        catch (InvalidOperationException)
                        {
                        }
                        catch (SecurityException)
                        {
                        }
                    }
                    if ((s_hostName == null) || (s_hostName.Length == 0))
                    {
                        s_hostName = s_notAvailableText;
                    }
                }
                return s_hostName;
            }
        }

        public static string NewLine
        {
            get
            {
                return Environment.NewLine;
            }
        }

        public static string NotAvailableText
        {
            get
            {
                return s_notAvailableText;
            }
            set
            {
                s_notAvailableText = value;
            }
        }

        public static string NullText
        {
            get
            {
                return s_nullText;
            }
            set
            {
                s_nullText = value;
            }
        }

        public static DateTime ProcessStartTime
        {
            get
            {
                return s_processStartTime;
            }
        }
    }
}

